<?php
namespace app\common\model;

use think\Model;

/**
 * 管理员关联分组的视图模型
 * Class AdminUser
 * @package app\common\model
 */
class AdminUserGroup extends Model
{

    /**
     * 查询一条用户信息
     *
     * @param array $params
     * @param bool $fields
     * @return array
     */
    public function getInfo(array $params, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['username']) && !empty($params['username'])) {
            $where['username'] = $params['username'];
        }

        if (!empty($where)) {
            $res = $this->where($where)->field($fields)->find();
            if (!empty($res)) {
                $return = $res->toArray();
            }
        }

        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['username']) && !empty($params['username'])) {
            $where['username'] = $params['username'];
        }

        if (isset($params['username_like']) && !empty($params['username_like'])) {
            $where['username'] = ['like', '%'.$params['username_like'].'%'];
        }

        if (isset($params['status']) && is_numeric($params['status'])) {
            $where['status'] = $params['status'];
        }

        if (isset($params['group_status']) &&  is_numeric($params['group_status'])) {
            $where['group_status'] = $params['group_status'];
        }

        if (isset($params['code']) &&  !empty($params['code'])) {
            $where['code'] = $params['code'];
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit);

        return $data->toArray();
    }

    /**
     * 查询列表,手动分页，防止框架 paginate 对于page会形成全局作用范围的问题
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getListByPage(array $params, array $limit = [], $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['username']) && !empty($params['username'])) {
            $where['username'] = $params['username'];
        }

        if (isset($params['username_like']) && !empty($params['username_like'])) {
            $where['username'] = ['like', '%'.$params['username_like'].'%'];
        }

        if (isset($params['status']) && is_numeric($params['status'])) {
            $where['status'] = $params['status'];
        }

        if (isset($params['group_status']) &&  is_numeric($params['group_status'])) {
            $where['group_status'] = $params['group_status'];
        }

        if (isset($params['code']) &&  !empty($params['code'])) {
            $where['code'] = $params['code'];
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        if (empty($limit)) {
            $limit = build_limit(1, 20);
        }

        $data = $this->where($where)->field($fields)->order($order)->limit($limit['begin'], $limit['offset'])->select();

        return $data->toArray();
    }
}